package com.roadjava.tutor.service.impl;

import com.roadjava.tutor.bean.dto.OrderInfoDTO;
import com.roadjava.tutor.bean.entity.Member;
import com.roadjava.tutor.bean.entity.OrderInfo;
import com.roadjava.tutor.bean.enums.OrderStatusEnum;
import com.roadjava.tutor.bean.res.ResultDTO;
import com.roadjava.tutor.bean.vo.OrderInfoVO;
import com.roadjava.tutor.bean.vo.TeacherJobVO;
import com.roadjava.tutor.mapper.OrderInfoMapper;
import com.roadjava.tutor.service.OrderInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhaodaowen
 * @see <a href="http://www.roadjava.com">乐之者java</a>
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Override
    public ResultDTO<String> add(OrderInfoDTO dto) {
        OrderInfo entity = dto.toEntity();
        int count = orderInfoMapper.insert(entity);
        if (count != 1) {
            return ResultDTO.buildFailure("预约失败");
        }else {
            return ResultDTO.buildSuccess("预约成功");
        }
    }

    @Override
    public ResultDTO<List<OrderInfoVO>> loadTable(OrderInfoDTO dto) {
        List<OrderInfoVO> list = orderInfoMapper.listByPage(dto);
        if (CollectionUtils.isEmpty(list)) {
            return ResultDTO.buildSuccess(list, (long) list.size());
        }
        // 转成状态描述
        list.forEach(vo -> vo.setStatusDesc(OrderStatusEnum.getDescByStatus(vo.getStatus())));
        long count = orderInfoMapper.pageCount(dto);
        return ResultDTO.buildSuccess(list,count);
    }

    @Override
    public ResultDTO<String> update(OrderInfoDTO dto) {
        OrderInfo entity = dto.toEntity();
        int count = orderInfoMapper.update(entity);
        if (count != 1) {
            return ResultDTO.buildFailure("更新失败");
        }else {
            return ResultDTO.buildSuccess("更新成功");
        }
    }
}
